<?php 
include 'includes/cPlayer.php';
include 'includes/cMember.php';
include 'includes/cDatabase.php';
include 'includes/variables.php';
include 'includes/cGame.php';
include 'includes/cDates.php';
include 'includes/cPage.php';

define("TD_DEFAULT", "<td rowspan='n' colspan='n' width='n' align='center|right' valign='top|bottom' bgcolor='n'>");
define("TD_JPEG", "<td width='50' align='center'>");
define("TD_POSITION", "<td width='50' align='center'>");
define("TD_CODE", "<td width='30' align='center'>");
define("TD_NAME", "<td width='100' align='left'>");
define("TD_TEAM", "<td width='10' align='center'>");
define("TD_VALUE", "<td width='10' align='left'>");
define("TD_SCORE", "<td width='50' align='center'>");
define("TD_HEADING", "<td rowspan='1' colspan='8' align='center' valign='top'>");
define("TD_CAT", "<td width='200' align='center'>");

$cPage = new page("Team Of The Week");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$thisWeekMemberScore = array();
$currPlayerCodeList = array();
$cPlayer = array();

$cGame = new game;
$cGame->calculatePositionsPerWeek();

$cDates = new dates;
$currentWeek = $cDates->getCurrentWeek();

$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$maxWeeks = $cDatabase->GetMaxWeeks($connect);

if ($currentWeek < 1)
{
	echo "<BR>Season has not yet started";
}
else if ($currentWeek > $maxWeeks)
{
	echo "<BR>Season has ended";
}
else
{
	// Get all scores for this week in the local array
	for ($i = 0; $i < $cGame->getNumOfMembers(); $i++)
	{
		$thisWeekMemberScore[$i] = $cGame->cMember[$i]->getScoreForWeek($currentWeek-1);
	}
	rsort($thisWeekMemberScore);
	// Loop all members until we find the member with the highest score for this weekm should head up the array
	for ($i = 0; $i < $cGame->getNumOfMembers(); $i++)
	{
		if ($cGame->cMember[$i]->getScoreForWeek($currentWeek-1) == $thisWeekMemberScore[0])
			$ourMember = $i;
	}

	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, 'memberName', $cGame->cMember[$ourMember]->getMemberName(), NULL, NULL);

	$finfo = mysqli_fetch_fields($result);
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

	for ($i = 1; $i <= MAXPLAYERS; $i++)
	{
		$playerCodeVariable = getPlayerCodeVariable($i);
		$playerWeekInVariable = getPlayerWeekInVariable($i);
		$playerWeekOutVariable = getPlayerWeekOutVariable($i);

		if ($row[$playerCodeVariable] != NULL)
		{
			$cPlayer[$i] = new player($row[$playerCodeVariable]);
			$error |= $cPlayer[$i]->setPlayerWeekIn($row[$playerWeekInVariable]);
			$error |= $cPlayer[$i]->setPlayerWeekOut($row[$playerWeekOutVariable]);
			$error |= $cPlayer[$i]->setPlayerName($connect);
			$error |= $cPlayer[$i]->setPlayerTeam($connect);
			$error |= $cPlayer[$i]->setPlayerPosition($connect);
			$error |= $cPlayer[$i]->setPlayerValue($connect);
			$error |= $cPlayer[$i]->setPlayerActive($connect);

			if ($cPlayer[$i]->getPlayerCode() != NULL)							// Is this a valid player
			{
				$cPlayer[$i]->calculatePlayerScore($connect);
				if ($cPlayer[$i]->getPlayerWeekIn() <= $currentWeek-1)
				{
					if ( ($cPlayer[$i]->getPlayerWeekOut() > $currentWeek-1) || ($cPlayer[$i]->getPlayerWeekOut() == NULL) )
					{
						$currPlayerCodeList[$k] = $cPlayer[$i]->getPlayerCode();	// Put it into the curr array
						$k++;
						$error |= $cGame->cMember[$ourMember]->addPlayerValue($cPlayer[$i]->getPlayerValue());
						$error |= $cGame->cMember[$ourMember]->addPlayerScore($cPlayer[$i]->getPlayerScore($maxWeeks), 'CURR');
						$error |= $cGame->cMember[$ourMember]->addFormation( $cPlayer[$i]->getPlayerPosition() );
					}
				}
			}
		}
	}
	sort($currPlayerCodeList);
		
	echo "<table width='1000' border='1'>";
	echo "<tr>".TD_CAT;
	echo BLUE."<span class='style1'><strong>Member: ".$cGame->cMember[$ourMember]->getMemberName()."</strong></span></td>";
	echo TD_CAT.BLUE."<span class='style1'><strong>Team: ".$cGame->cMember[$ourMember]->getTeamName();

	echo "</strong></span></font></td></tr>";
		echo "<tr>";
			echo "<td rowspan='12' colspan='8'>";
				echo "<table>";
					for ($i = 0; $i < 11; $i++)
					{
						for ($l = 1; $l <= MAXPLAYERS; $l++)
						{
							if ($cPlayer[$l]->getPlayerCode() == $currPlayerCodeList[$i])
							{	// We've found the player in the cPlayer structure
								break;
							}
						}
							echo "<tr>";
								// This player is in the current lineup
								$path = $cPlayer[$l]->shirtLookup();
								echo TD_JPEG."<img src='".$path."' width='50' height='50'></td>";
								echo TD_POSITION.BLUE.$cPlayer[$l]->getPlayerPosition()."</td>";	// Position
								echo TD_CODE.BLUE.$cPlayer[$l]->getPlayerCode()."</td>";			// Code
								echo TD_NAME.BLUE.$cPlayer[$l]->getPlayerName()."</td>";			// Name
								echo TD_TEAM.BLUE.$cPlayer[$l]->getPlayerTeam()."</td>";			// Team
								echo TD_VALUE.BLUE.$cPlayer[$l]->getPlayerValue()."m</td>";			// Value
								if ($cPlayer[$l]->getPlayerScoreForWeek($currentWeek-1) == NULL)
									echo TD_SCORE.BLUE."-</font></td>";	// Score
								else
									echo TD_SCORE.BLUE.$cPlayer[$l]->getPlayerScoreForWeek($currentWeek-1)."</font></td>";	// Score			
								echo "<td>&nbsp;</td>";
							echo "</tr>";
						}
							echo "<tr>";
								echo "<td></td>";
							?>
								<td colspan='3'>
								<table border='0'>
									<tr>
										<td colspan="2" align="center">

										</td>
									</tr>
								</table>
								</td>
							<?php
							echo "<td colspan='2'>WEEK ".($currentWeek-1)." POINTS</td>";
							echo TD_SCORE."<strong>".$cGame->cMember[$ourMember]->getScoreForWeek($currentWeek-1)."</strong></td>";
						echo "</tr>";
					echo "</table>";
				echo "</td>";
				echo "<td>";
					echo "<table>";
						echo "<td><img src='JPGraph\DB_TeamStatsWeeklyScoresGraph?memberName=".$cGame->cMember[$ourMember]->getMemberName()."' border=0 align=center width=500 height=300></td>";
					echo "</table>";
				echo "</td>";
			echo "</tr>";
		
		// Other information table
		echo "<tr>";
			echo "<td><table>";
				echo "<td><img src='JPGraph\DB_TeamStatsWeeklyPositionGraph?memberName=".$cGame->cMember[$ourMember]->getMemberName()."' border=0 align=center width=500 height=300></td>";
			echo "</table></td>";
		echo "</tr>";
		
	echo "</table>";
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?> 